package work.chenbo.minio.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.*;
import io.minio.errors.*;
import okhttp3.Headers;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Demo1 {
    public static void main(String[] args) {
        try {
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://139.198.26.118:9000")
                    .credentials("admin","gogojiayou1314")
                    .build();

            BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket("image").build();

            boolean b = minioClient.bucketExists(bucketExistsArgs);

            if(!b){
                MakeBucketArgs makeBucketArgs = MakeBucketArgs.builder().bucket("image").build();
                minioClient.makeBucket(makeBucketArgs);
            }else {
                System.out.println("bucket [/image] already exists");
            }

            ObjectWriteResponse image = minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("image")
                            .object("2404d68afb3a15b253f0118989de2ebb.jpeg")
                            .filename("C:\\Users\\chenbo\\Desktop\\2404d68afb3a15b253f0118989de2ebb.jpeg")
                            .build()
            );

            System.out.println("文件上传成功");
            Headers headers = image.headers();
            String bucket = image.bucket();
            String region = image.region();
            System.out.println(headers.toString());
            System.out.println(bucket.toString());
            System.out.println(region.toString());

        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        }


    }
}
